# -*- coding:utf-8 -*-
from flask import request, jsonify, abort
from flask_cors import CORS
from com.zzh.dao_v2.adminRoleDao import AdminRoleDao

from . import app

CORS(app, supports_credentials=True)


@app.route('/insertAdminRole', methods=['POST'])
def insert_admin_role():
    """
    添加信息
    :return:
    """
    if not request.json:
        abort(400)
    info = {
        'roleName': request.json.get('roleName', ''),
        'roleType': request.json.get('roleType', ''),
        'roleDesc': request.json.get('roleDesc', ''),
        'roleIsSys': request.json.get('roleIsSys', 0)
    }

    info = AdminRoleDao().insertAdminRole(info)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/findAdminRoleById', methods=['POST'])
def find_admin_role_by_id():
    """
    根据ID查询
    :return:
    """
    if not request.json:
        abort(400)
    roleId = request.json.get('roleId', '')
    info = AdminRoleDao().findAdminRoleById(roleId)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 1, "status": "error"}})


@app.route('/findAdminRoleByCondition', methods=['POST'])
def find_admin_role_by_condition():
    """
    条件查询
    :return:
    """
    if not request.json:
        abort(400)
    try:
        page = request.json['page']
        pageIndex = page["pageIndex"]
        pageSize = page["pageSize"]
    except:
        return jsonify({"message": {"code": 0, "status": "请传入 pageIndex和pageSize"}})

    try:
        conditions = request.json['condition']
    except:
        return jsonify({"message": {"code": 0, "status": "未传入指定condition"}})
    try:
        multiSort = request.json['multiSort']
    except:
        return jsonify({"message": "未传入排序规则multiSort"})
    info, total = AdminRoleDao().findAdminRoleByConditon(pageIndex=pageIndex, pageSize=pageSize, conditions=conditions,
                                                         multiSort=multiSort)

    if info:
        return jsonify({"total": total, "info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"info": [{}], "message": {"code": 1, "status": "error"}})


@app.route('/deleteAdminRoleByIds', methods=['POST'])
def delete_admin_role_by_ids():
    """
    根据ID删除，ID以数组形式传递
    :return:
    """
    if not request.json:
        abort(400)
    idArray = request.json.get('idArray', [0])

    info = AdminRoleDao().deleteAdminRole(idArray)
    if info:
        return jsonify({"message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})


@app.route('/updateAdminRoleById', methods=['POST'])
def update_admin_role_by_id():
    """
    更新
    :return:
    """
    if not request.json:
        abort(400)
    try:
        roleId = request.json['roleId']
    except:
        return jsonify({"message": {"code": 0, "status": "请传入要修改信息的ID"}})
    oldInfo = AdminRoleDao().selectAdminRole(roleId)
    info = {
        'roleId': request.json['roleId'],
        'roleName': request.json.get('roleName', oldInfo[1]),
        'roleType': request.json.get('roleType', oldInfo[2]),
        'roleDesc': request.json.get('roleDesc', oldInfo[3]),
        'roleIsSys': request.json.get('roleIsSys', oldInfo[4])
    }

    info = AdminRoleDao().updateAdminRole(info)

    if info:
        return jsonify({"info": info, "message": {"code": 1, "status": "success"}})
    else:
        return jsonify({"message": {"code": 0, "status": "error"}})
